package com.originskyseed.platform.task.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;

import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.tenant.util.TenantCodeUtil;
import com.originskyseed.platform.core.util.bean.PrimaryKeyGeneratorUtil;
import com.originskyseed.platform.task.mapper.SpreadLimitMapper;
import com.originskyseed.platform.task.model.SpreadLimit;
import com.originskyseed.platform.task.service.ISpreadLimitService;
import com.originskyseed.platform.user.mapper.UserInfoMapper;
import com.originskyseed.platform.user.model.UserInfo;

/**
 * 推广上限记录表Service接口实现类（OSI_SPREAD_LIMIT）
 *
 * @author wlj
 */
@DubboService
@Component("spreadLimitService")
public class SpreadLimitServiceImpl implements ISpreadLimitService {

	@Resource
	private SpreadLimitMapper spreadLimitMapper;

	@Resource
	private UserInfoMapper userInfoMapper;

	/**
	 * 删除记录
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKey(String id) throws Exception {
		return this.spreadLimitMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 批量删除记录
	 *
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKeys(List<String> ids) throws Exception {
		return this.spreadLimitMapper.deleteByPrimaryKeys(ids);
	}

	/**
	 * 批量修改状态
	 *
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateStateByIds(List<String> ids, int state) throws Exception {
		return this.spreadLimitMapper.updateStateByIds(ids, state);
	}

	/**
	 * 插入记录
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insert(SpreadLimit record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.spreadLimitMapper.insert(record);
	}

	/**
	 * 插入记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertSelective(SpreadLimit record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.spreadLimitMapper.insertSelective(record);
	}

	/**
	 * 新增记录
	 *
	 * @param spreadLimit
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertSelectiveSave(SpreadLimit spreadLimit, UserInfo userInfo) throws Exception {
		spreadLimit.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		spreadLimit.setCreateTime(new Date());
		spreadLimit.setCreatorId(userInfo.getId());// 创建人ID
		spreadLimit.setCreator(userInfo.getName());// 创建人名称
		spreadLimit.setState(2);
		if (userInfo.getSourceSys() == 2) {
			spreadLimit.setType(3);
		} else if (userInfo.getSourceSys() == 1) {
			spreadLimit.setType(2);
		} else {
			spreadLimit.setType(1);
		}
		spreadLimit.setRemainNum(spreadLimit.getNum());
		// 如果是OW、运管可传入多个绑定对象ID
		if (userInfo.getSourceSys() == 0) {
			if (StringUtils.isNotBlank(spreadLimit.getObjId())) {
				String objIds = spreadLimit.getObjId();
				List<String> objId = Arrays.asList(objIds.split(","));
				for (String s : objId) {
					UserInfo info = this.userInfoMapper.selectByPrimaryKey(s);
					spreadLimit.setObjId(s);
					spreadLimit.setObjName(info.getName());
					this.insertSelective(spreadLimit);
				}
			} else {
				this.insertSelective(spreadLimit);
			}

		} else {
			this.insertSelective(spreadLimit);
		}
		return 1;
	}

	/**
	 * 新增系统默认
	 *
	 * @param spreadLimit
	 * @param userInfo
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertSelectiveSaveDefault(SpreadLimit spreadLimit, UserInfo userInfo) throws Exception {
		spreadLimit.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		spreadLimit.setCreateTime(new Date());
		spreadLimit.setCreatorId(userInfo.getId());// 创建人ID
		spreadLimit.setCreator(userInfo.getName());// 创建人名称
		spreadLimit.setState(2);
		if (userInfo.getSourceSys() == 1) {
			spreadLimit.setObjId("-1");
			spreadLimit.setObjName("-1");
		} else {
			spreadLimit.setObjId("-2");
			spreadLimit.setObjName("-2");
		}
		spreadLimit.setRemainNum(spreadLimit.getNum());
		return this.insertSelective(spreadLimit);
	}

	/**
	 * 更新记录
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKey(SpreadLimit record) throws Exception {
		record.setUpdateTime(new Date());
		return this.spreadLimitMapper.updateByPrimaryKey(record);
	}

	/**
	 * 每个月1号更新推广上限为设置值
	 *
	 * @param state
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateNumSetValue(int state, String tenantCode) throws Exception {
		if (StringUtils.isNotEmpty(tenantCode)) {
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		return this.spreadLimitMapper.updateNumSetValue(state);
	}

	/**
	 * 接单上限统计
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateCountById(String id) throws Exception {
		return this.spreadLimitMapper.updateCountById(id);
	}

	/**
	 * 接单数量统计-1
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateSubtractById(String id) throws Exception {
		return this.spreadLimitMapper.updateSubtractById(id);
	}

	/**
	 * 通过对象ID接单数量统计 加
	 *
	 * @param id
	 * @param number
	 * @return
	 * @throws Exception
	 */
	@Override
	public int CountByObjId(String id, int number) throws Exception {
		return this.spreadLimitMapper.CountByObjId(id, number);
	}

	/**
	 * 通过对象ID接单数量统计 减
	 *
	 * @param id
	 * @param number
	 * @return
	 * @throws Exception
	 */
	@Override
	public int SubtractByObjId(String id, int number) throws Exception {
		return this.spreadLimitMapper.SubtractByObjId(id, number);
	}

	/**
	 * 更新记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKeySelective(SpreadLimit record) throws Exception {
		record.setUpdateTime(new Date());
		return this.spreadLimitMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 修改设置上限的默认值
	 *
	 * @param num
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateNumDefault(int num) throws Exception {
		return this.spreadLimitMapper.updateNumDefault(num);
	}

	/**
	 * 查询记录
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public SpreadLimit selectByPrimaryKey(String id) throws Exception {
		return this.spreadLimitMapper.selectByPrimaryKey(id);
	}

	/**
	 * 通过对象ID查询记录
	 *
	 * @param objId
	 * @return
	 * @throws Exception
	 */
	@Override
	public SpreadLimit selectByObjId(String objId) throws Exception {
		return this.spreadLimitMapper.selectByObjId(objId);
	}

	/**
	 * 查询记录
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public SpreadLimit selectBySpreadLimit(SpreadLimit record) throws Exception {
		return this.spreadLimitMapper.selectBySpreadLimit(record);
	}

	/**
	 * 查询列表
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<SpreadLimit> selectList(SpreadLimit record) throws Exception {
		return this.spreadLimitMapper.selectList(record);
	}

	/**
	 * 分页查询列表
	 *
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<SpreadLimit> selectListByPage(Map<String, Object> params, BasePage<SpreadLimit> page) throws Exception {
		params.put("page", page);
		List<SpreadLimit> list = this.spreadLimitMapper.selectListByPage(params);
		page.setResults(list);
		return page;
	}

	@Override
	public BasePage<SpreadLimit> selectGroupListByPage(Map<String, Object> params, BasePage<SpreadLimit> page) throws Exception {
		params.put("page", page);
		List<SpreadLimit> list = this.spreadLimitMapper.selectGroupListByPage(params);
		page.setResults(list);
		return page;
	}

}
